<!DOCTYPE html>
<html>
<head>
<include file="Public:meta"/>
<script type="text/javascript">
	
	//扩展验证规则
	$.extend($.fn.validatebox.defaults.rules, {
		mobile: {
			validator: function(value, param){
				return isMobile(value);
			},
			message: '请输入格式正确的手机号！'
		}
	});
    $(function(){
		var fetchUrl = "{:U('Content/categoryList')}?act=ajax";
		$('#categoryList').tree({
			url:fetchUrl,
			title:'文章分类列表',
			method: 'get',
			animate: true,
			lines:true,
			dnd:true,
			onDblClick: function(node){
				if(node.id != 1){
					$(this).tree('beginEdit',node.target);
				}
			},
			onContextMenu: function(e,node){
				e.preventDefault();
				$(this).tree('select',node.target);
				$('#mm').menu('show',{
					left: e.pageX,
					top: e.pageY
				});
			},
			onAfterEdit:function(node){
				var text = node.text;
				var save = true;
				var parent = 0;
				var node_id = 0;
				parent = node.parent;
				if(node.id == null){
					if(text == '' || text == '新增分类名称'){
						save = false;
						$(this).tree('remove', node.target);
					}
				}else{
					if(text == ''){
						save = false;
						var selnode = $(this).tree('getSelected');
						if(selnode != null){
							$(this).tree('cancelEdit',selnode.target);
							$(this).tree('uncheck',selnode.target);
						}
						$(this).tree('beginEdit',node.target);
					}
					node_id = node.id;
				}
				if(save){
					var saveUrl = "{:U('Content/categorySave')}";
					var data = {id:node_id,text:text,parent:parent};
					$.get(saveUrl,data,function(result){
						if(result == 1)
						{
							var treeList = $('#categoryList');
							var pnode = treeList.tree('getParent', node.target);
							treeList.tree('expand',pnode.target);
							treeList.tree('expand',node.target);
							
						}else{
							alert('保存出错！');	
							$('#categoryList').tree('reload');
						}
					},'text');	
				}
			}
		});
		
    });
	
	//新增分类
	function append(){
		var t = $('#categoryList');
		var node = t.tree('getSelected');
		t.tree('expand', node.target);
		t.tree('append', {
			parent: (node?node.target:null),
			data: [{
				text: '新增分类名称',
				parent: node.id				//父节点id
			}]
		});
	}
	function removeit(){
		var node = $('#categoryList').tree('getSelected');
		var cat_id = node.id;
		if(cat_id != 1){
			if(cat_id > 0){
				var delUrl = "{:U('Content/categoryDel')}?act=del";
				$.get(delUrl,{id:cat_id},function(result){
					if(result == 1){
						$('#categoryList').tree('remove', node.target);
					}else if(result == 2){
						alert("该分类下还有子分类，不能删除！");
					}else if(result == 3){
						alert("该分类下还文章，不能删除！");
					}else{
						alert("删除失败！");
					}
				},'text');
			}else{
				$('#categoryList').tree('remove', node.target);
			}
		}else{
			alert("根节点不能删除！");
		}
	}
	//生成二维码
	function makeQRCode(){
		var node = $('#categoryList').tree('getSelected');
		var cat_id = node.id;
		var jumpUrl = "{:U('QRCode/makeQRCodeForUrl')}?node="+cat_id+"&type=category";
		window.parent.addTab("生成二维码", jumpUrl);
	}
	
	//设置顺序
	function sortCate()
	{
		var node = $('#categoryList').tree('getSelected');
		var cat_id = node.id;
		var sortUrl = "{:U('Content/setCateSort')}";
		if(cat_id != 1){
			$.get(sortUrl,{cid:cat_id},function(data){
				if(data == 1){
					$("#categoryList").tree('reload');
				}else{
					alert("操作出错！");
				}
			});
		}else{
			alert("根节点不能置顶操作！");
		}
	}
	
</script>

</head>
<body>
    <div class="easyui-panel" title="文章分类管理"> 
     	<div style="margin:20px 25px; border:1px dashed #efefef; font-style:italic;">
            <p>
            1、双击分类名称可以修改名称；<br/>
            2、鼠标右键点击分类名称可以新增子分类、置顶排序、生成二维码等。
            </p>
        </div>
    	<ul id="categoryList" class="easyui-tree" style="margin:20px;"></ul>
    </div>
    
    <div id="mm" class="easyui-menu" style="width:120px;">
        <div onclick="append()" data-options="iconCls:'icon-add'">新增子分类</div>
        <div onclick="sortCate()" data-options="iconCls:'icon-sort'">置顶到第一位</div>
        <div onclick="makeQRCode()" data-options="iconCls:'icon-qrcode'">生成二维码</div>
		<div onclick="removeit()" data-options="iconCls:'icon-remove'">删除该分类</div>
	</div>
    
    <div id="set-sort" class="easyui-dialog" title="设置分类顺序" data-options="closed:true,buttons:'#dlg-buttons'" style="width:300px; height:150px; padding:20px;">
    	<select class="easyui-combobox" id="set-sort-val" name="set-sort-val"  panelHeight="auto" style="width:100%;">
        	<option value="1">置顶至第1位</option>
            <option value="2">移动到第2位</option>
            <option value="3">移动到第3位</option>
            <option value="4">移动到第4位</option>
            <option value="5">移动到第5位</option>
            <option value="-5">移动到倒数第5位</option>
            <option value="-4">移动到倒数第4位</option>
            <option value="-3">移动到倒数第3位</option>
            <option value="-2">移动到倒数第2位</option>
            <option value="-1">移至最后1位</option>
        </select>
        
    </div>
    <div id="dlg-buttons" style="text-align:center;">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save" onclick="javascript:saveSort()">确 定</a>
	</div>

</body>
</html>
